<?php
// No directAcces
defined('_AppBackMiage') or die('Restricted access');
if(isAdministrateur()){
    include_once (DS . "backoffice/include/header.php");
    include_once( DS."backoffice/include/toppanel_administration.php");

    $showOK = false;
    $showError = false;

if (isset($_GET['id']) && !empty($_GET['id'])) {
        $doc = new TypeDocument($_GET['id']);

    if(isset($_POST['submit']) && !empty($_POST['submit'])){
        // variable qui va nous servir pour savoir s'il y a a eu une erreur
        $form = true;
        $errMSG = array();

        //MAJ du nouveau type document
        //Verification et MAJ du nom du document
        if (isset($_POST['name']) && !empty($_POST['name'])) {
            $tmp = $doc->setNomType($_POST['name']);
            if ($tmp['boolOk'] === false) {
                $errMSG['name'] = $tmp['errmsg'];
                $form = false;
            }
        }

        //Verification et MAJ de la date limite de rendu du doc
        if (isset($_POST['deadline']) && !empty($_POST['deadline'])) {
            $conversion = Tools::transformDateToTimestamp($_POST['deadline']);
            $tmp = $doc->setDeadline($conversion);

            if ($tmp['boolOk'] === false) {
                $errMSG['deadline'] = $tmp['errmsg'];
                $form = false;
            }
        }

        // Vérification et MAJ de la liste des promos auxquelles est proposé le stage
        if (isset($_POST['promo']) && !empty($_POST['promo'])) {
            $tmp = $doc->setPromo($_POST['promo']);

            if ($tmp['boolOk'] === false) {
                $errMSG['promo'] = $tmp['errmsg'];
                $form = false;
        }
    }

    //sauvegarde du nouveau type de document
    if ($form === true) {
        $doc->save();
        if ($doc->requeteOk === true) {
            $showOK = true;
        } else {
            $showError = true;
        }
    }
    }
}

?>
    <div id="wrapper">
        <div id="content">
            <div id="box">
                <h3 id="adduser">Ajouter un document obligatoire</h3>
                    <?php if ($showError): ?>
                        <div class="error">Une erreur est survenue. Veuillez re-essayer ult&eacute;rieurement.</div>
                    <?php endif; ?>
                    <?php if ($showOK): ?>
                        <div class="success">Le type document a &eacute;t&eacute; modifi&eacute;.</div>
                    <?php endif; ?>
                <form method="post" id="form"  enctype="multipart/form-data">
                        <fieldset>
                    <label for="name">Nom :</label>
                    <input name="name" id="name" type="text" tabindex="1" value="<?php echo stripslashes($doc->getNomType()); ?>" />
                    <br />
                    <label for="deadline">Deadline : </label>
                    <input type="text" class="text datepicker" name="deadline" id="deadline" value="<?php echo date('j/n/y', $doc->getDeadline()); ?>"/>
                    <br />
                        <label for="promo">Promo : </label>
                        <table id="tabSansBordure">
                        <?php
                            $i = 0;
                            $lstPromo = Promo::getListPromo();
                            $promo = $doc->getPromo();
                            foreach ($lstPromo as $k => $e) {
                                if ($i == 0) {
                                    echo "<tr>";
                                }
                        ?>
                                <td style="border-right:1px #999 solid;">
                                    <label for="promo-<?php echo $e; ?>" style="font-weight:normal;">
                                <?php echo $e; ?>
                            </label>
                                    <input type="radio" name="promo" id="promo-<?php echo $e; ?>" value="<?php echo $k; ?>" 
                                    <?php if ($promo == "$k") echo "checked' "; ?>/>
                        </td>
                        <?php
                                if ($i == 4) {
                                    echo "</tr>";
                                    $i = 0;
                                } else {
                                    $i++;
                                }
                            }
                        ?>
                        </table>
                    <br />
                    </fieldset>
                <div align="center">
                    <input id="button1" type="submit" name="submit" value="Modifier" />
                </div>
                </form>
            </div>
 
        </div>
    <?php
        include_once (DS . "backoffice/include/sidebar.php");
        include_once (DS . "backoffice/include/footer.php");
}
else
{
    redirectTo(URLBACK);
}
?>